package views;

import DAO.scTabDao;
import Entity.scTab;
import util.Clear_txt;
import util.NotNull;
import util.Result;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.SQLException;
import java.util.List;

public class TourismAlliance {
    JLabel l0;
    JTextField txt0;
    JLabel l1;
    JLabel l2;
    JLabel l3;
    JTextField txt3;
    JLabel l4;
    JTextField txt4;
    JLabel l5;
    JTextField txt5;
    JLabel l6;
    JTextField txt6;
    JLabel l7;
    JTextField txt7;
    JTextField txtrest;
    JTextField txtin;
    JTextField txtout;
    JButton btnOK;
    JButton btn3;

    scTabDao scTabDao=new scTabDao();
    scTab sc=new scTab();


    class PanelBg extends  JPanel{
        Dimension d;
        Image image;
        PanelBg(Dimension d,Image image){
            this.d=d;
            this.image=image;
        }
        @Override
        public void paintComponent(Graphics g){
            g.drawImage(image,0,0,d.width,d.height,this);
        }
    }
    Dimension fmsize=new Dimension(600,700);
    ImageIcon imageicon = new ImageIcon(this.getClass().getResource("/erhai4.jpg"));
    TourismAlliance(Result result){
        JFrame jframe = new JFrame();
        jframe.setSize(600,700);
        jframe.setDefaultCloseOperation(jframe.DISPOSE_ON_CLOSE);
        jframe.setLayout(null);
        jframe.setLocationRelativeTo(null);


        PanelBg pContent = new PanelBg(fmsize,imageicon.getImage());
        pContent.setLayout(null);
        pContent.setBounds(0,0,600,700);
        jframe.getContentPane().add(pContent);
        jframe.setFocusable(true);



        l0 = new JLabel("景区编号");
        l0.setBounds(50,50,100,30);
        pContent.add(l0);

        txt0 = new JTextField();
        txt0.setBounds(150,50,200,30);
        txt0.setForeground(Color.lightGray);
        pContent.add(txt0);
        txt0.setEnabled(false);
        txt0.setText("编号自动生成");


        l1 = new JLabel("景区名称");
        l1.setBounds(50,100,100,30);
        pContent.add(l1);

        txtrest = new JTextField();
        txtrest.setBounds(150,100,200,30);
        pContent.add(txtrest);


        l2 = new JLabel("所在省市");
        l2.setBounds(50,150,100,30);
        pContent.add(l2);

        txtin = new JTextField();
        txtin.setBounds(150,150,200,30);
        pContent.add(txtin);

        l3 = new JLabel("景区联系人电话");
        l3.setBounds(50,200,100,30);
        pContent.add(l3);

        txt3 = new JTextField();
        txt3.setBounds(150,200,200,30);
        pContent.add(txt3);

        l4 = new JLabel("网点经手人");
        l4.setBounds(50,250,100,30);
        pContent.add(l4);

        txt4 = new JTextField();
        txt4.setBounds(150,250,200,30);
        pContent.add(txt4);


        l5 = new JLabel("门票价格");
        l5.setBounds(50,300,100,30);
        pContent.add(l5);

        txt5 =new JTextField();
        txt5.setBounds(150,300,200,30);
        pContent.add(txt5);



        l6 = new JLabel("景区开放时间");
        l6.setBounds(50,350,100,30);
        pContent.add(l6);

        txt6 = new JTextField();
        txt6.setBounds(150,350,200,30);
        pContent.add(txt6);


        l7 = new JLabel("景区特色项目");
        l7.setBounds(50,400,100,30);
        pContent.add(l7);

        txt7 = new JTextField();
        txt7.setBounds(150,400,200,30);
        pContent.add(txt7);

        //初始化文本框
        if(result.getStatus().equals("0")){
            String scNo=""+result.getMessage();
            sc=scTabDao.get_byID(scNo);
            txt0.setText(sc.getScNo());
            txtrest.setText(sc.getScName());
            txtin.setText(sc.getScP());
            txt3.setText(sc.getScTel());
            txt4.setText(sc.getScRespon());
            txt5.setText(sc.getScPrice());
            txt6.setText(sc.getScTime());
            txt7.setText(sc.getScSpecial());
        }

        btn3 =new JButton("保存");
        btn3.setBounds(100,500,70,30);
        pContent.add(btn3);
        btn3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(result.getStatus().equals("1")) //代表新增
                {
                    try {
                        int id=Integer.parseInt(scTabDao.justgetID())+1;
                        sc.setScNo(id+"");
                        sc.setScName(txtrest.getText().trim());
                        sc.setScP(txtin.getText().trim());
                        sc.setScTel(txt3.getText().trim());
                        sc.setScRespon(txt4.getText().trim());
                        sc.setScPrice(txt5.getText().trim());
                        sc.setScTime(txt6.getText().trim());
                        sc.setScSpecial(txt7.getText().trim());
                        if(!scTabDao.exit_Name(sc.getScName())){
                            return;
                        }
                        if (!new NotNull().IsNull(txtrest,txtin,txt3)||!new NotNull().IsNull(txt4,txt5,txt6)){
                            JOptionPane.showMessageDialog(null,"不能为空");
                            return;
                        }
                        scTabDao.insert(sc);
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
                else {
                    sc.setScName(txtrest.getText().trim());
                    sc.setScP(txtin.getText().trim());
                    sc.setScTel(txt3.getText().trim());
                    sc.setScRespon(txt4.getText().trim());
                    sc.setScPrice(txt5.getText().trim());
                    sc.setScTime(txt6.getText().trim());
                    sc.setScSpecial(txt7.getText().trim());
                    scTabDao.update3(sc);
                }
                JOptionPane.showMessageDialog(null,"保存成功");
                Clear_txt clear_txt=new Clear_txt();
                clear_txt.clear_txt(txtrest,txtin);
                clear_txt.clear_txt(txt3,txt4);
                clear_txt.clear_txt(txt5,txt6);
                clear_txt.clear_txt(txt7,txt7);
            }
        });


        btnOK = new JButton("重置");
        btnOK.setBounds(400,500,70,30);
        pContent.add(btnOK);
        btnOK.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txt0.setText("");
                txtrest.setText("");
                txtin.setText("");
                txt3.setText("");
                txt4.setText("");
                txt5.setText("");
                txt6.setText("");
                txt7.setText("");
            }
        });


        jframe.setVisible(true);

    }

//    public static void main(String[] args) {
//        Result result=new Result();
//        result.setStatus("1");
//        String id="3";
//        result.setMessage(id);
//        TourismAlliance tourismAlliance=new TourismAlliance(result);
//    }
}
